package com.cloudptt.api.product.func.call;

import java.util.List;

import com.cloudptt.api.comm.CauseCont;
import com.cloudptt.api.product.ISuperCallOperator;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.addr.IAddrOp;
import com.cloudptt.api.product.func.call.group.HalfCallInfo;
import com.cloudptt.api.product.func.call.group.GroupCallSpeakListener;
import com.cloudptt.api.product.func.call.group.GroupCallTracker;
import com.cloudptt.api.product.func.call.single.SingleCallTracker;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.CallOperator;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.ServerInfo;
import com.mcptt.db.model.AddrInfo;

import android.content.Context;



public class CallTracker implements IFunc,ISuperCallOperator,ICallMute
{
	private final static String THIS_FILE = "CallTracker";
	private GroupCallTracker groupCallTracker = null;
	private SingleCallTracker singleCallTracker = null;
	private CallOperator operator = null;

	public CallTracker(Context ctx,McIdInfo mcIdInfo,IAddrOp addOp,CallOperator operator)
	{
		this.groupCallTracker = new GroupCallTracker(ctx,mcIdInfo,addOp,operator,this);
		this.singleCallTracker = new SingleCallTracker(ctx,mcIdInfo,addOp,operator,this);
		this.operator = operator;
	}

	public int newCall(CallAttr callAttr)
	{
		Log.d(THIS_FILE, "newCall  callAttr = " + callAttr);
		if(operator != null)
		{
			int rusult = operator.checkOperate();
			if(CauseCont.OperateCheck_Suc != rusult)
			{
				return rusult;
			}
		}
		int callType = callAttr.getCallType();
		if(callType == CallAttr.CallType_Group
				|| callType == CallAttr.CallType_Single_half)
		{
			return groupCallTracker.newCall(callAttr);
		}
		else if(callType == CallAttr.CallType_Single
				|| callType == CallAttr.CallType_Video_Push
				|| callType == CallAttr.CallType_Video_Pull
				
				|| callType == CallAttr.CallType_Video_Trans
				|| callType == CallAttr.CallType_Video_TransGrp
				|| callType == CallAttr.CallType_Video_PushGrp)
		{
			return singleCallTracker.newCall(callAttr);
		}
		return CauseCont.OperateCheck_Call_CallTypeErr;
	}

	public int doUserAction(String callKey,int userAction)
	{
		Log.d(THIS_FILE, "doUserAction callKey = " + callKey + " userAction = " + CallConst.userActionTag(userAction));
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		Log.d(THIS_FILE, "doUserAction groupCallTracker isHasCall = " + isHasCall);
		if(isHasCall
				&& userAction != CallConst.BtnAction_Make_Video)
		{
			return groupCallTracker.doUserAction(callKey,userAction);
		}
		else
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			Log.d(THIS_FILE, "doUserAction pCallTracker isHasCall = " + isHasCall);
			if(isHasCall)
			{
				return singleCallTracker.doUserAction(callKey, userAction);
			}
		}
		return CauseCont.OperateErr_NoAgent;
	}
	
	
	
	public boolean isHasCall(String callKey)
	{
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		if(!isHasCall)
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			Log.d(THIS_FILE, "isHasCall isHasCall = " + isHasCall);
			return isHasCall;
//			return isHasCall;
		}
		return isHasCall;
	}
	
	public boolean isMicMute(String callKey)
	{
		boolean isHasCall = groupCallTracker.isHasCall(callKey);
		if(isHasCall)
		{
			return true;
		}
		else
		{
			isHasCall = singleCallTracker.isHasCall(callKey);
			if(isHasCall)
			{
				return singleCallTracker.isMicMute(callKey);
			}
		}
		return true;
	}
	
	
	public int getPCallState(String callKey)
	{
		return singleCallTracker.getWebrtcCallState(callKey);
	}
	
	public boolean isHaveActivePCall()
	{
		return singleCallTracker.isHaveActivePCall();
	}
	
//	public boolean isWebrtcCallMicMute(String callKey)
//	{
//		return webrtcCallTracker.isMicMute(callKey);
//	}
//	
//	public boolean isPCallMicMute(String callKey)
//	{
//		return singleCallTracker.isMicMute(callKey);
//	}
	public HalfCallInfo getHalfCallInfo(String callKey)
	{
		HalfCallInfo info = groupCallTracker.getCallInfo(callKey);
		return info;
	}
	
	public List<HalfCallInfo> getGroupCallInfoList()
	{
		List<HalfCallInfo> list = groupCallTracker.getTalkInfoList();
		return list;
	}
	
	public String getCallDesc()
	{
		return groupCallTracker.getCallDesc() + singleCallTracker.getCallDesc();
	}
	public String getSysInfoStr()
	{
		return groupCallTracker.getSysInfoStr() + singleCallTracker.getSysInfoStr();
	}
	@Override
	public boolean isActiveCall(String callKey) {
		// TODO Auto-generated method stub
		boolean isActiveCall = groupCallTracker.isActiveCall(callKey);
		if(!isActiveCall)
		{
			isActiveCall = singleCallTracker.isActiveCall(callKey);
			return isActiveCall;
		}
		return isActiveCall;
	}

//	@Override
//	public SurfaceViewRenderer getWebrtcCallSurfaceView(String callKey, int type)
//	{
//		return webrtcCallTracker.getSurfaceView(callKey, type);
//	}
//	@Override
//	public SurfaceViewRenderer getPCallSurfaceView(String callKey, int type)
//	{
//		return singleCallTracker.getSurfaceView(callKey, type);
//	}
//	public void setSurfaceView(String callKey, SurfaceViewRenderer localRender, SurfaceViewRenderer remoteRender) {
//		// TODO Auto-generated method stub
//		webrtcCallTracker.setSurfaceView(callKey, localRender, remoteRender);
//	}

	public void setPCallClass(Class cls)
	{
		singleCallTracker.setPrivacyClass(cls);
	}
	public void setDefaultGroupInfo(AddrInfo info)
	{
		groupCallTracker.setDefaultGroupInfo(info);
	}
	
	public void setSingleCallListener(CallListener callListener)
	{
		Log.d(THIS_FILE, "McpttCallTracker setPCallListener");
		singleCallTracker.setListener(callListener);
	}
	public void setGCallListener(CallListener callListener,GroupCallSpeakListener speakListener)
	{
		Log.d(THIS_FILE, "McpttCallTracker setGCallListener");
		groupCallTracker.setListener(callListener,speakListener);
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub

	}
	@Override
	public void onLogined(McIdInfo mcIdInfo,ServerInfo info,boolean isLoginSuc)
	{
		Log.d(THIS_FILE, "onLogined isLoginSuc = " + isLoginSuc);
		if(!isLoginSuc)
		{
			doForceDetach();
		}
	}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(THIS_FILE, "doNetworkChange isConn = " + isConn);
		groupCallTracker.doNetworkChange(isConn);
		singleCallTracker.doNetworkChange(isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.d(THIS_FILE, "doForceDetach");
		groupCallTracker.doForceDetach();
		singleCallTracker.doForceDetach();
	}
	@Override
	public void doLogout()
	{
		Log.d(THIS_FILE, "doLogout");
		groupCallTracker.doLogout();
		singleCallTracker.doLogout();
	}
	@Override
	public void doExit()
	{
		Log.d(THIS_FILE, "CallAgent exit");
		groupCallTracker.doExit();
		singleCallTracker.doExit();
	}
	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		groupCallTracker.onKeepAlive(isconnected);
		singleCallTracker.onKeepAlive(isconnected);
	}
	
	public void startSoundPlayer(int type)
	{
		groupCallTracker.startSoundPlayer(type);
	}
	
	public void setCallHistory(ICallHistory callHistory) {
		groupCallTracker.setCallHistory(callHistory);
		singleCallTracker.setCallHistory(callHistory);
	}

	public String toString() 
	{
		StringBuffer strBuff = new StringBuffer("McpttCall:");
		//		strBuff.append("callAttr = ").append(callAttr).append(",");
		//		strBuff.append("callAgent = ").append(callAgent).append(";");

		return strBuff.toString();
	}

	@Override
	public CallOperator getCallOperator() {
		// TODO Auto-generated method stub
		return operator;
	}

	@Override
	public void muteCall(boolean isOn) {
		// TODO Auto-generated method stub
		boolean isHave = singleCallTracker.isHaveActivePVoiceCall();
		Log.d(THIS_FILE, "muteCall isHave = " + isHave + " isOn = " + isOn);
		if(isHave && isOn)
		{
			groupCallTracker.muteCall(true);
		}
		if(!isOn)
		{
			groupCallTracker.muteCall(false);
		}
	}

	@Override
	public void screenAngle(String callKey, int angle) {
		// TODO Auto-generated method stub
		singleCallTracker.screenAngle(callKey,angle);
	}

	@Override
	public void negotiateRtcpChannel()
	{
		groupCallTracker.negotiateRtcpChannel();
	}

//	@Override
//	public void setNoSnd() {
//		// TODO Auto-generated method stub
//		Log.d(THIS_FILE, "setNoSnd");
//		if(groupCallTracker != null)
//		{
//			groupCallTracker.setNoSnd();
//		}
//	}
//	@Override
//	public void setSnd(boolean isMust)
//	{
//		// TODO Auto-generated method stub
//		Log.d(THIS_FILE, "setNoSnd");
//		if(groupCallTracker != null)
//		{
//			groupCallTracker.setSnd(isMust);
//		}
//	}
}
